package com.cs2340.practice;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

public class LoginActivity extends Activity {

	/** Called when the activity is first created. */

	private static final String TAG = "MyActivity";
	private UserDataBaseAdapter myUserDatabaseAdapter;
	private TaskDataBaseAdapter myTaskDatabaseAdapter;
	private LocationDataBaseAdapter myLocationDataBaseAdapter;
	private Validator validate;
	EditText user;
	EditText pass;
	EditText email;

	
	EditText retypePass;
	TextView errorText;
	ListView listOtasks;
    TextView loginErrorText;
    
    int temp = 0;


	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		
		validate = new Validator();
		
		myUserDatabaseAdapter = new UserDataBaseAdapter(this);
		
		//myUserDatabaseAdapter.openToWrite();
		//myUserDatabaseAdapter.deleteAll();
		//myUserDatabaseAdapter.insert("john", "smith");
		//myUserDatabaseAdapter.close();

	}

	public void newUser(View view) {
		
		temp++;
		Log.i(TAG, "New users created: " + temp);
		
		user = (EditText) findViewById(R.id.usernameInput);
		pass = (EditText) findViewById(R.id.passwordInput);
		email = (EditText) findViewById(R.id.emailInput);
		retypePass = (EditText) findViewById(R.id.retypePasswordInput);
		errorText = (TextView) findViewById(R.id.errorText);

		String username = user.getText().toString();
		String password = pass.getText().toString();
		String emailAddrs = email.getText().toString();
		String retypePassword = retypePass.getText().toString();
		

		
		myUserDatabaseAdapter = new UserDataBaseAdapter(this);
		myUserDatabaseAdapter.openToWrite();
		ArrayList<String> existingUsernames = myUserDatabaseAdapter.getUsernames();

		if (validate.usernameExists(existingUsernames,username)) {
			errorText.setText(R.string.usernameExists);
		} else {

			if (!validate.passwordMatch(password, retypePassword)) {
				// show passwords not match text
				errorText.setText(R.string.passwordNoMatch);
			} else {
				// username password has been validated
				myUserDatabaseAdapter.insert(username, password, emailAddrs);
				setContentView(R.layout.login);
			} // end else

		} // end else
		// for demo use only
		// for demo use only don't try at home
		myUserDatabaseAdapter.close();
		Log.i(TAG, "Usernames in database: " + existingUsernames.toString());
	}

	/** onClick for going to the login screen. */
	public void gotoLogin(View view) {
		setContentView(R.layout.login);
	}

	/** onClick for going to the new account screen. */
	public void gotoNewAccount(View view) {
		setContentView(R.layout.newaccount);
	}
	
	/** onClick for clearing database. */
	public void clearDB(View view) {
		myUserDatabaseAdapter = new UserDataBaseAdapter(this);
		myUserDatabaseAdapter.openToWrite();
		myUserDatabaseAdapter.deleteAll();
		myUserDatabaseAdapter.close();
		
		Log.i(TAG, "clearing user database: " );
		
		myTaskDatabaseAdapter = new TaskDataBaseAdapter(this);
		myTaskDatabaseAdapter.openToWrite();
		myTaskDatabaseAdapter.deleteAll();
		myTaskDatabaseAdapter.close();
		
		Log.i(TAG, "clearing task database: " );
		
		myLocationDataBaseAdapter = new LocationDataBaseAdapter(this);
		myLocationDataBaseAdapter.openToWrite();
		myLocationDataBaseAdapter.deleteAll();
		myLocationDataBaseAdapter.close();
		
		Log.i(TAG, "clearing location database: " );
		
	}


	/** onClick for going to the main screen. */
	public void gotoMain(View view) {
		setContentView(R.layout.main);
	}

	/** onClick for going to task list view. */
	public void gotoTaskListView(String username) {
		Bundle basket = new Bundle();
		basket.putString("userKey", username);
	//	Intent openTaskList = new Intent(LoginActivity.this, TaskActivity.class);
		Intent openTaskList = new Intent( "android.intent.action.VIEW_TASKS");
		openTaskList.putExtras(basket);
		startActivity(openTaskList);

	}

	/**
	 * Checks to see if login is valid, if it is, logs the user in.
	 */
	public void login(View view) {
		user = (EditText) findViewById(R.id.usernameInput);
		pass = (EditText) findViewById(R.id.passwordInput);
		String username = user.getText().toString();
		String password = pass.getText().toString();
		
		loginErrorText = (TextView) findViewById(R.id.loginErrorText);
		
		myUserDatabaseAdapter = new UserDataBaseAdapter(this);
		myUserDatabaseAdapter.openToWrite();
		ArrayList<String> existingUsernames = myUserDatabaseAdapter.getUsernames();
		String databasePassword= myUserDatabaseAdapter.getPassword(username);
		
		if (validate.usernameExists(existingUsernames, username)) {
			if (validate.checkPassword(password, databasePassword)) {
				gotoTaskListView( username);
			} else {
				loginErrorText.setText(R.string.wrongUsernameorPassword);
			} // end else
		} else {
			loginErrorText.setText(R.string.wrongUsernameorPassword);
		} // end else
		
		myUserDatabaseAdapter.close();

	} // end login

}